#include "circlechartpage.h"
#include "ui_circlechartpage.h"

CircleChartPage::CircleChartPage(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::CircleChartPage),
    temperatureWarningCount(0),
    temperatureAlarmCount(0),
    humidityWarningCount(0),
    humidityAlarmCount(0),
    lightWarningCount(0),
    lightAlarmCount(0)
{
    ui->setupUi(this);

    // 初始化温度饼状图
    temperatureSeries = new QPieSeries();
    temperatureWarningSlice = temperatureSeries->append("温度预警 (40-60)", 0);
    temperatureAlarmSlice = temperatureSeries->append("温度告警 Alarm (>60)", 0);

    temperatureChart = new QChart();
    temperatureChart->addSeries(temperatureSeries);
    temperatureChart->setTitle("Temperature Warning and Alarm Data");
    temperatureWarningSlice->setLabelVisible();
    temperatureAlarmSlice->setLabelVisible();
    ui->temperatureChartView->setChart(temperatureChart);

    // 初始化湿度饼状图
    humiditySeries = new QPieSeries();
    humidityWarningSlice = humiditySeries->append("湿度预警 (40-60)", 0);
    humidityAlarmSlice = humiditySeries->append("湿度告警 (>60)", 0);

    humidityChart = new QChart();
    humidityChart->addSeries(humiditySeries);
    humidityChart->setTitle("Humidity Warning and Alarm Data");
    humidityWarningSlice->setLabelVisible();
    humidityAlarmSlice->setLabelVisible();
    ui->humidityChartView->setChart(humidityChart);

    // 初始化光照饼状图
    lightSeries = new QPieSeries();
    lightWarningSlice = lightSeries->append("光照预警 (40-60)", 0);
    lightAlarmSlice = lightSeries->append("光照告警 (>60)", 0);

    lightChart = new QChart();
    lightChart->addSeries(lightSeries);
    lightChart->setTitle("Light Warning and Alarm Data");
    lightWarningSlice->setLabelVisible();
    lightAlarmSlice->setLabelVisible();
    ui->lightChartView->setChart(lightChart);
}

CircleChartPage::~CircleChartPage()
{
    delete ui;
}

void CircleChartPage::addTemperatureData(int value)
{
    qDebug() << "into addTemperatureData";
    if (value > 60) {
        temperatureAlarmCount++;
        temperatureAlarmSlice->setValue(temperatureAlarmCount);
        temperatureAlarmSlice->setLabel(QString("温度告警(>60): %1").arg(temperatureAlarmCount));
    } else if (value > 40) {
        temperatureWarningCount++;
        temperatureWarningSlice->setValue(temperatureWarningCount);
        temperatureWarningSlice->setLabel(QString("温度预警(40-60): %1").arg(temperatureWarningCount));
    }
}

void CircleChartPage::addHumidityData(int value)
{
       qDebug() << "into addHumidityData";
    if (value > 60) {
        humidityAlarmCount++;
        humidityAlarmSlice->setValue(humidityAlarmCount);
        humidityAlarmSlice->setLabel(QString("湿度告警(>60): %1").arg(humidityAlarmCount));
    } else if (value > 40) {
        humidityWarningCount++;
        humidityWarningSlice->setValue(humidityWarningCount);
        humidityWarningSlice->setLabel(QString("湿度预警(40-60): %1").arg(humidityWarningCount));
    }
}

void CircleChartPage::addLightData(int value)
{
     qDebug() << "into addLightData";
    if (value > 60) {
        lightAlarmCount++;
        lightAlarmSlice->setValue(lightAlarmCount);
        lightAlarmSlice->setLabel(QString("光照告警(>60): %1").arg(lightAlarmCount));
    } else if (value > 40) {
        lightWarningCount++;
        lightWarningSlice->setValue(lightWarningCount);
        lightWarningSlice->setLabel(QString("光照预警(40-60): %1").arg(lightWarningCount));
    }
}
